package com.ezhuan.system.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.alibaba.fastjson.JSONObject;
import com.ezhuan.common.service.BaseServiceImpl;
import com.ezhuan.common.util.ConstatFinalUtil;
import com.ezhuan.common.util.MultiThread;
import com.ezhuan.common.util.PageInfoUtil;
import com.ezhuan.system.dao.IAAdverDao;
import com.ezhuan.system.dao.IACateDao;
import com.ezhuan.system.dao.IAChannelDao;
import com.ezhuan.system.dao.IARegionDao;
import com.ezhuan.system.dao.IASmsHistoryDao;
import com.ezhuan.system.dao.IASuggestDao;
import com.ezhuan.system.dao.IAVersionDao;
import com.ezhuan.system.dao.impl.AAdverDaoImpl;
import com.ezhuan.system.dao.impl.ACateDaoImpl;
import com.ezhuan.system.dao.impl.AChannelDaoImpl;
import com.ezhuan.system.dao.impl.ARegionDaoImpl;
import com.ezhuan.system.dao.impl.ASmsHistoryDaoImpl;
import com.ezhuan.system.dao.impl.ASuggestDaoImpl;
import com.ezhuan.system.dao.impl.AVersionDaoImpl;
import com.ezhuan.system.pojo.AAdver;
import com.ezhuan.system.pojo.ACate;
import com.ezhuan.system.pojo.AChannel;
import com.ezhuan.system.pojo.ARegion;
import com.ezhuan.system.pojo.ARegionEnum;
import com.ezhuan.system.pojo.ASmsHistory;
import com.ezhuan.system.pojo.ASmsHistoryEnum;
import com.ezhuan.system.pojo.ASuggest;
import com.ezhuan.system.pojo.AVersion;
import com.ezhuan.system.service.ISystemService;
import com.ezhuan.users.pojo.AUsers;
import com.ezhuan.users.service.IUsersService;

/**
 * 数据模块的实现类
 * 
 * @author wangshh
 * 
 */
public class SystemServiceImplay extends BaseServiceImpl implements ISystemService
{
	private IARegionDao regionDao = new ARegionDaoImpl();
	private IASmsHistoryDao smsHistoryDao = new ASmsHistoryDaoImpl();
	private IASuggestDao suggestDao = new ASuggestDaoImpl();
	private IAChannelDao channelDao = new AChannelDaoImpl();
	private IAAdverDao adverDao = new AAdverDaoImpl() ;
	private IAVersionDao versionDao = new AVersionDaoImpl() ; 
	private IACateDao cateDao = new ACateDaoImpl() ; 
	
	/* 防止对象之间相互调用,依赖 */
	private IUsersService usersService = (IUsersService) ConstatFinalUtil.BEANMAP.get("usersService");
	
	@Override
	public JSONObject saveOneRegionService(ARegion region)
	{
		JSONObject resultJSON = new JSONObject();
		/* 处理树形结构 */
		Map<String, Object> condMap = new HashMap<String,Object>();
		condMap.put("id", region.getParentId());
		ARegion regionRes = this.findOneRegionService(condMap);
		if(regionRes != null)
		{
			/* 更新父节点的叶子节点 */
			if(regionRes.getLeafStatus() == ARegionEnum.LEAFSTATUS_FALSE.getStatus())
			{
				regionRes.setLeafStatus(ARegionEnum.LEAFSTATUS_TRUE.getStatus());
				regionRes.setUpdateTime(new Date());
				this.updateOneRegionService(regionRes);
			}
		}
		
		boolean flag = this.regionDao.save(region);
		if (flag)
		{
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", region.getId() + "");
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		} else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneRegionService(ARegion region)
	{
		JSONObject resultJSON = new JSONObject();
		boolean flag = this.regionDao.update(region);
		if (flag)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		} else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON ; 
	}

	@Override
	public JSONObject deleteOneRegionService(ARegion region)
	{
		return null ; 
	}

	@Override
	public List<ARegion> findCondListRegionService(PageInfoUtil pageInfoUtil , Map<String, Object> condMap)
	{
		return this.regionDao.findCondListDao(pageInfoUtil, condMap); 
	}

	@Override
	public ARegion findOneRegionService(Map<String, Object> condMap)
	{
		ARegion region = this.regionDao.findOneDao(condMap);
		return region ; 
	}

	@Override
	public List<ARegion> findTreeRegionService(ARegion region)
	{
		List<ARegion> regionList = new ArrayList<ARegion>(); 
		if(region != null)
		{
			this.findTreeRegionServiceUtil(regionList,region);
			Collections.reverse(regionList);
			
			//获得字符串表示
			StringBuffer ressb = new StringBuffer();
			for (Iterator iterator = regionList.iterator(); iterator.hasNext();)
			{
				ARegion regTemp = (ARegion) iterator.next();
				ressb.append(regTemp.getName() + ConstatFinalUtil.SPLIT_STR);
			}
			
			if(!"".equalsIgnoreCase(ressb.toString()))
			{
				ressb.delete(ressb.lastIndexOf(ConstatFinalUtil.SPLIT_STR), ressb.length());
			}
			region.setTreeName(ressb.toString());
		}
		return regionList ; 
	}
	
	/**
	 * 计算树形结构的工具方法,一级节点-->二级节点-->三级节点
	 * @param ressb
	 * @param regionid
	 */
	private void findTreeRegionServiceUtil(List<ARegion> regList, ARegion region)
	{
		if(region == null)
		{
			return ;
		}
		regList.add(region);
		if(region.getParentId() != 0)
		{
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("id", region.getParentId() + "");
			ARegion regionRes = this.findOneRegionService(condMap);
			//ARegion regionRes = ConstatFinalUtil.regionMap.get(region.getParentid());
			findTreeRegionServiceUtil(regList, regionRes);
		}
	}
	
	@Override
	public ASmsHistory findOneSmsHistoryService(Map<String, Object> condMap)
	{
		ASmsHistory smsHistory = this.smsHistoryDao.findOneDao(condMap);
		if(smsHistory != null && 
				"true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			Map<String, Object> extMap = new HashMap<String,Object>();
		}
		return smsHistory ; 
	}

	@Override
	public List<ASmsHistory> findCondListSmsHistoryService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<ASmsHistory> smsHistoryList = this.smsHistoryDao.findCondListDao(pageInfoUtil, condMap);
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			Map<String, Object> extMap = new HashMap<String,Object>();
			for (Iterator iterator = smsHistoryList.iterator(); iterator.hasNext();)
			{
				ASmsHistory smsHistory = (ASmsHistory) iterator.next();
			}
		}
		return smsHistoryList ; 
	}

	@Override
	public JSONObject saveOneSmsHistoryService(ASmsHistory smsHistory)
	{
		JSONObject resultJSON = new JSONObject();
		/* 同一个手机号,在60秒以后,不能重复发送短信,不管是成功还是失败 */
		Calendar calendar = Calendar.getInstance() ;
		Date edDate = calendar.getTime() ; 
		calendar.add(Calendar.MINUTE, -1);
		Date stDate = calendar.getTime() ; 
		
		Map<String, Object> condMap = new HashMap<String,Object>();
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);
		condMap.put("phone", smsHistory.getPhone());
		
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		List<ASmsHistory> smsHistoryList = this.findCondListSmsHistoryService(pageInfoUtil, condMap);
		/* 只要找到1条,就报错 */
		if(pageInfoUtil.getTotalRecord() > 0)
		{
			resultJSON.put("code", "8");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("8"));
			return resultJSON ; 
		}
		
		/* 保存记录 */
		boolean flag = this.smsHistoryDao.save(smsHistory);
		if(flag)
		{
			/* 采用 多线程来发送短信 */
			MultiThread multiThread = new MultiThread();
			Map<String,Object> paramsMap = new HashMap<String, Object>();
			paramsMap.put("operType", "sendSms");
			paramsMap.put("systemService", this);
			paramsMap.put("smsHistory", smsHistory);
			multiThread.setParamsMap(paramsMap);
			
			/* 启用多线程 */
			ExecutorService executorService = Executors.newFixedThreadPool(1);
			try
			{
				/* 提交线程,不等待 */
				executorService.submit(multiThread);
			} catch (Exception e)
			{
			}finally
			{
				executorService.shutdown();
			}
			
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", smsHistory.getId() + "");
			dataJSON.put("sn", smsHistory.getSn() + "");
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneSmsHistoryService(ASmsHistory smsHistory)
	{
		JSONObject resultJSON = new JSONObject();
		
		/* 保存记录 */
		boolean flag = this.smsHistoryDao.update(smsHistory);
		if(flag)
		{
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", smsHistory.getId() + "");
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}
	
	@Override
	public JSONObject updateSendSmsService(ASmsHistory smsHistory)
	{
		/* 发送短信的工具类 */
		JSONObject resJSON = smsUtil.sendSms(smsHistory.getPhone(), smsHistory.getCode(), smsHistory.getTemplateSn());
		if("0".equalsIgnoreCase(resJSON.getString("code")))
		{
			/* 短信发送成功 */
			/* 数据库操作暂时不考虑 */
			
			/* 给下行返回信息 */
			JSONObject dataResJSON = (JSONObject) resJSON.get("data") ; 
			String thirdSn = dataResJSON.getString("thirdSn") ; 
			
			smsHistory.setThirdsn(thirdSn);
			smsHistory.setStatus(ASmsHistoryEnum.STATUS_SUCCED.getStatus());
			smsHistory.setUpdateTime(new Date());
		}else
		{
			smsHistory.setStatus(ASmsHistoryEnum.STATUS_FAILED.getStatus());
			smsHistory.setUpdateTime(new Date());
		}
		/* 更新数据 */
		JSONObject resDBJSON = this.updateOneSmsHistoryService(smsHistory);
		return resDBJSON;
	}
	
	@Override
	public ASuggest findOneSuggestService(Map<String, Object> condMap)
	{
		ASuggest suggest = this.suggestDao.findOneDao(condMap);
		if(suggest != null &&
				"true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			Map<String, Object> extMap = new HashMap<String,Object>();
			
			/* 为扩展字段赋值 */
			/* 为用户赋值 */
			extMap.put("id", suggest.getUsersId());
			AUsers users = this.usersService.findOneUsersService(condMap);
			suggest.setUsersObject(users);
		}
		return suggest ; 
	}

	@Override
	public List<ASuggest> findCondListSuggestService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<ASuggest> suggestList = this.suggestDao.findCondListDao(pageInfoUtil, condMap);
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			Map<String, Object> extMap = new HashMap<String,Object>();
			for (Iterator iterator = suggestList.iterator(); iterator.hasNext();)
			{
				ASuggest suggest = (ASuggest) iterator.next();
				
			}
		}
		return suggestList ; 
	}

	@Override
	public JSONObject saveOneSuggestService(ASuggest suggest)
	{
		JSONObject resultJSON = new JSONObject();
		/* 保存记录 */
		boolean flag = this.suggestDao.save(suggest);
		if(flag)
		{
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", suggest.getId() + "");
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneSuggestService(ASuggest suggest)
	{
		JSONObject resultJSON = new JSONObject();
		/* 保存记录 */
		boolean flag = this.suggestDao.update(suggest);
		if(flag)
		{
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", suggest.getId() + "");
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}
	
	@Override
	public JSONObject saveOneChannelService(AChannel channel)
	{
		JSONObject resultJSON = new JSONObject();
		/* 保存记录 */
		boolean flag = this.channelDao.save(channel);
		if(flag)
		{
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", channel.getId() + "");
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneChannelService(AChannel channel)
	{
		JSONObject resultJSON = new JSONObject();
		/* 保存记录 */
		boolean flag = this.channelDao.update(channel);
		if(flag)
		{
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", channel.getId() + "");
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}

	@Override
	public AChannel findOneChannelService(Map<String, Object> condMap)
	{
		AChannel Channel = this.channelDao.findOneDao(condMap);
		if(Channel != null && 
				"true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			Map<String, Object> extMap = new HashMap<String,Object>();
		}
		return Channel ; 
	}

	@Override
	public List<AChannel> findCondListChannelService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<AChannel> ChannelList = this.channelDao.findCondListDao(pageInfoUtil, condMap);
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			Map<String, Object> extMap = new HashMap<String,Object>();
			for (Iterator iterator = ChannelList.iterator(); iterator.hasNext();)
			{
				AChannel Channel = (AChannel) iterator.next();
				
			}
		}
		return ChannelList ; 
	}
	
	@Override
	public AAdver findOneAdverService(Map<String, Object> condMap)
	{
		AAdver adver = this.adverDao.findOneDao(condMap);
		if(adver != null && 
				"true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			Map<String, Object> extMap = new HashMap<String,Object>();
		}
		return adver ; 
	}

	@Override
	public List<AAdver> findCondListAdverService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<AAdver> adverList = this.adverDao.findCondListDao(pageInfoUtil, condMap);
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			Map<String, Object> extMap = new HashMap<String,Object>();
			for (Iterator iterator = adverList.iterator(); iterator.hasNext();)
			{
				AAdver adver = (AAdver) iterator.next();
				
			}
		}
		return adverList ; 
	}

	@Override
	public JSONObject saveOneAdverService(AAdver adver)
	{
		JSONObject resultJSON = new JSONObject();
		/* 保存记录 */
		boolean flag = this.adverDao.save(adver);
		if(flag)
		{
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", adver.getId() + "");
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneAdverService(AAdver adver)
	{
		JSONObject resultJSON = new JSONObject();
		/* 保存记录 */
		boolean flag = this.adverDao.update(adver);
		if(flag)
		{
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", adver.getId() + "");
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}
	
	@Override
	public AVersion findOneVersionService(Map<String, Object> condMap)
	{
		AVersion version = this.versionDao.findOneDao(condMap);
		if(version != null && 
				"true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			Map<String, Object> extMap = new HashMap<String,Object>();
		}
		return version ; 
	}

	@Override
	public List<AVersion> findCondListVersionService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<AVersion> versionList = this.versionDao.findCondListDao(pageInfoUtil, condMap);
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			Map<String, Object> extMap = new HashMap<String,Object>();
			for (Iterator iterator = versionList.iterator(); iterator.hasNext();)
			{
				AVersion version = (AVersion) iterator.next();
			}
		}
		return versionList ; 
	}

	@Override
	public JSONObject saveOneVersionService(AVersion version)
	{
		JSONObject resultJSON = new JSONObject();
		/* 保存记录 */
		boolean flag = this.versionDao.save(version);
		if(flag)
		{
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", version.getId() + "");
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneVersionService(AVersion version)
	{
		JSONObject resultJSON = new JSONObject();
		/* 保存记录 */
		boolean flag = this.versionDao.update(version);
		if(flag)
		{
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", version.getId() + "");
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}
	
	@Override
	public ACate findOneCateService(Map<String, Object> condMap)
	{
		ACate cate = this.cateDao.findOneDao(condMap);
		if(cate != null && 
				"true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			Map<String, Object> extMap = new HashMap<String,Object>();
		}
		return cate ; 
	}

	@Override
	public List<ACate> findCondListCateService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<ACate> cateList = this.cateDao.findCondListDao(pageInfoUtil, condMap);
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			Map<String, Object> extMap = new HashMap<String,Object>();
			for (Iterator iterator = cateList.iterator(); iterator.hasNext();)
			{
				ACate cate = (ACate) iterator.next();
			}
		}
		return cateList ; 
	}

	@Override
	public JSONObject saveOneCateService(ACate cate)
	{
		JSONObject resultJSON = new JSONObject();
		/* 保存记录 */
		boolean flag = this.cateDao.save(cate);
		if(flag)
		{
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", cate.getId() + "");
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneCateService(ACate cate)
	{
		JSONObject resultJSON = new JSONObject();
		/* 保存记录 */
		boolean flag = this.cateDao.update(cate);
		if(flag)
		{
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", cate.getId() + "");
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}
}
